//
//  LoginTableViewCell.m
//  CommunityNannyO2O
//
//  Created by 90 on 15/7/21.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "LoginTableViewCell.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h" 	
#import "AccountTool.h"
#import "CustomTextField.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "LoginModel.h"
#import "UserDataSingleton.h"
#import "CommFunc.h"


@interface LoginTableViewCell()<QXNetWorkToolDelegate>

@property (weak, nonatomic) IBOutlet CustomTextField  *phone;

@property (weak, nonatomic) IBOutlet CustomTextField *verificationCode;

@property (weak, nonatomic) IBOutlet UIButton *login;

@property (weak, nonatomic) IBOutlet UIButton *regist;

@property (weak, nonatomic) IBOutlet UIButton *forgetPasssword;

@property (weak, nonatomic) IBOutlet UIButton *backBtn;

@property (weak, nonatomic) IBOutlet UIView *backView;


@end


@implementation LoginTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"LoginTableViewCell";
    LoginTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:ID owner:nil options:nil]firstObject];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    return cell;
}

-(void)awakeFromNib
{
    //叉号，一次性删除输入框中的内容
    [self.phone setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.verificationCode setClearButtonMode:UITextFieldViewModeWhileEditing];
    
    //编辑时清空
    self.phone.clearsOnBeginEditing = YES;
    self.verificationCode.clearsOnBeginEditing = YES;
    
    //placeholder字体、颜色
    UIFont *font = [UIFont fontWithName:@"Hiragino Sans GB" size:13];
    UIColor *color = UIColorRGBA(235, 235, 235, 1);
    
    [self.phone setFont:font Color:color];
    [self.verificationCode setFont:font Color:color];
    
    //账号叉号
    UIButton *clearButton;
    clearButton = [self.phone valueForKey:@"_clearButton"];
    [clearButton setImage:[UIImage imageNamed:@"ico_login_del"]
                 forState:UIControlStateNormal];
    
    //密码叉号
    clearButton = [self.verificationCode valueForKey:@"_clearButton"];
    [clearButton setImage:[UIImage imageNamed:@"ico_login_del"]
                 forState:UIControlStateNormal];
    
    self.verificationCode.secureTextEntry = YES;
    
    //登录
    self.login.layer.cornerRadius = 16;
    self.login.layer.masksToBounds = YES;
    [self.login.layer setBorderWidth:1.0]; //边框宽度
    [self.login.layer setBorderColor:UIColorRGBA(255, 255, 255, 1).CGColor];//边框颜色
    [self.login addTarget:self action:@selector(lgoinSender:) forControlEvents:UIControlEventTouchUpInside];
    
    self.regist.layer.cornerRadius = 16;
    self.regist.layer.masksToBounds = YES;
    [self.regist.layer setBorderWidth:1.0]; //边框宽度
    [self.regist.layer setBorderColor:UIColorRGBA(255, 255, 255, 1).CGColor];//边框颜色
    [self.regist addTarget:self action:@selector(registSender:) forControlEvents:UIControlEventTouchUpInside];

    [self.forgetPasssword addTarget:self action:@selector(forgetPassswordSender:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [self addGestureRecognizer:tapGestureRecognizer];
    
    //子view 999
    UIView *containViewUp = [self viewWithTag: 999];
    
    UITapGestureRecognizer *tapGestureRecognizerUp = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containViewUp addGestureRecognizer:tapGestureRecognizerUp];
    
    //子view 888
    UIView *containViewDown = [self viewWithTag: 888];
    
    UITapGestureRecognizer *tapGestureRecognizerDown = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containViewDown addGestureRecognizer:tapGestureRecognizerDown];
    
    //back btn
    [self.backBtn addTarget:self action:@selector(backSender:) forControlEvents:UIControlEventTouchUpInside];
    
    //back view
    UITapGestureRecognizer *tapGestureRecognizerBack = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBack)];
    [self.backView addGestureRecognizer:tapGestureRecognizerBack];
}

- (BOOL)isCorrectPhone
{
    if ([self.phone.text isEqualToString:@""])
    {
        [MBProgressHUD showError:@"请输入手机号码或邮箱" toView:self];
        return NO;
    }
    
    if ([CommFunc isPureInt:self.phone.text])
    {
        if(![CommFunc isValidateMobile:self.phone.text])
        {
            [MBProgressHUD showError:@"请正确输入手机号" toView:self];
            
            return NO;
        }
    }
    else
    {
        if(![CommFunc isValidateEmail:self.phone.text])
        {
            [MBProgressHUD showError:@"请正确输入邮箱" toView:self];
            
            return NO;
        }
    }
    
    return YES;
}

- (void)lgoinSender:(id)sender
{
    if ([self isCorrectPhone] == NO)
    {
        return;
    }
    
    if (self.verificationCode.text.length <= 0)
    {
        [MBProgressHUD showError:@"请输入登录密码"];
        return;
    }
    
    [MBProgressHUD showMessage:@"正在加载"];
    
    QXNetWorkTool *tool = [[QXNetWorkTool alloc]init];

    tool.QXNetWorkDelegate = self;
    
    [tool GetDataFromServerWithPostMethodandInterfaceName:LoginInterface andParameter:@[@{@"login_name":self.phone.text,@"pwd":self.verificationCode.text,
           @"cid":[[UserDataSingleton shareInstance] getGeTuiClientID]}] tag:1];
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    switch (tag)
    {
        case 1:
        {
            if (Response == nil)
            {
                return;
            }
            
            for (NSDictionary *dic in Response)
            {
                LoginModel *model = [LoginModel LoginWithDict:dic];
                
                [[UserDataSingleton shareInstance] LoginWithUserId:model.id
                                                            MidiId:model.midi_id
                                                          NickName:model.nickname
                                                          RealName:model.real_name
                                                             Email:model.eamil
                                                             Phone:model.phone
                                                          National:model.nationality
                                                               Sex:model.sex
                                                            People:model.people
                                                            IdCard:nil
                                                          Birthday:nil
                                                           HeadImg:nil];
            }
            
            if ([self.loginDeletage respondsToSelector:@selector(login)]) {
                
                [self.loginDeletage login];
            }
            
            break;
        }
    }
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showError:message];
            break;
    }
}

- (void)registSender:(id)sender
{
    if ([self.registDeletage respondsToSelector:@selector(regist)]) {
        
        [self.registDeletage regist];
    }
}

- (void)forgetPassswordSender:(id)sender
{
    if ([self.forgetPasswordDeletage respondsToSelector:@selector(forgetPassword)]) {
        
        [self.forgetPasswordDeletage forgetPassword];
    }
}

- (void)backSender:(id)sender
{
    if ([self.loginDeletage respondsToSelector:@selector(back)]) {
        
        [self.loginDeletage back];
    }
}

-(void)tap
{
    [self.phone resignFirstResponder];
    [self.verificationCode resignFirstResponder];
}

-(void)tapBack
{
    if ([self.loginDeletage respondsToSelector:@selector(back)]) {
        
        [self.loginDeletage back];
    }
}


@end














